<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="good" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <!-- 一级部门的pid都是 '' -->
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置element-ul中的tree组件 -->
        <el-tree
          :data="list"
          :props="{children: 'children',label: 'label'}"
          default-expand-all
        >
          <template #default="{ data }">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="good" />
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑子部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="hDel(data.id)"
                        >
                          删除子部门
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <!-- dialog弹层 -->
        <el-dialog
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          :title="isEdit? '编辑部门':'新增部门'"
          :visible.sync="showDialog"
          @close="resetClose"
        >
          <!-- 子组件 -->
          <!-- 解决子组件数据不更新方式一 v-if="showDialog" -->
          <deptDialog
            :id="curId"
            ref="updateRef"
            :is-edit="isEdit"
            :origin-list="originList"
            @success="hSuccess"
            @close="hClose"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>
<script>
import { getDepartment, delDepartment } from '@/api/departments'
import { transDataToTreeData } from '@/utils/index'
import deptDialog from '@/views/departments/deptDialog.vue'
export default {
  components: { deptDialog },
  data() {
    return {
      showDialog: false, // 控制弹层显示与隐藏
      list: [], // 保存数据
      curId: '', // 保存Id
      isEdit: false, // 当为false则是添加 || 为true则是编辑
      originList: [] // 储存校验规则对象
    }
  },
  created() {
    // 页面刷新时渲染公司人员列表
    this.loadGetDepartment()
  },
  methods: {
    resetClose() {
      this.$refs.updateRef.resetform() // 通过子组件$refs来调用子组件中的函数
    },
    // 子传父事件函数
    hClose() {
      this.showDialog = false // 关闭弹层
    },
    // 实现删除部门
    hDel(id) {
      // 提示询问框
      this.$confirm('您确定要删除此部门吗?').then(async() => {
        const res = await delDepartment(id)
        if (res.code !== 10000) return this.$message.error(res.message) // 删除失败
        // 删除成功
        this.$message.success(res.message)
        // 重新渲染数据
        this.loadGetDepartment()
        // 关闭弹层
        this.showDialog = false
      }).catch((error) => {
        console.log(error)
      })
    },
    // 子传父事件函数
    hSuccess() {
      // 重新渲染数据
      this.loadGetDepartment()
      // 关闭弹层
      this.showDialog = false
    },
    // 编辑部门
    hEdit(id) {
      this.isEdit = true
      this.showDialog = true // 控制弹层开启
      this.curId = id // 收集id传给子组件使用
      // // 方式二 使用$refs与$nextTick方法
      // this.$nextTick(() => {
      //   this.$refs.updateRef.loadgetDepartmentById()
      // })
    },
    // 新增部门
    hAdd(id) {
      this.isEdit = false
      this.showDialog = true // 控制弹层开启
      this.curId = id // 收集id传给子组件使用
    },
    async loadGetDepartment() {
      try {
        const res = await getDepartment()
        this.originList = res.data.depts.map(item => {
          return { id: item.id, code: item.code, pid: item.pid, name: item.name }
        })
        console.log(res)
        res.data.depts.shift() // 第一个为公司名称我们不需要这条数据(删除)
        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
